<?php
namespace Badmins\Controller;
class FormsController extends CkadminController {
    public function index(){
       $this->display();
    }
    /**
     * 添加、编辑快递
     */
    public function exp(){
    	$id = (int)I('id');
    	if($id){
    		$db = M('express_list');
    		$list  = $db->where("id='{$id}'")->find();
    		$this->assign('va',$list);
    	}
    	$this->display();
    }
    /**
     * 保存快递
     */
    public function save_exp(){
    	$db = D('ExpressList');
    	if($create = $db->create()){
    		$id = I('id','','intval');
    		if($id){
    			$create['id'] = $id;
    			$res = $db->save($create);
    		}else{
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			$log = "添加了快递公司 ，公司名称为".$create['name'];
    			$this->add_log($log);
    			showmsg(200,'保存成功');
    		}else{
    			showmsg(200,'你没有做任何的修改，无需保存编辑');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 改变快递的状态
     */
    public function update_exp(){
    	$id = (int)I('id');
    	$db = M('express_list');
    	$res = $db->where("id='{$id}'")->field("name,status")->find();
    	if($res['status']){
    		$db->where("id='{$id}'")->setField('status',0);
    		$log = "将快递公司名为".$res['name']."的状态从禁用变成启用";
    	}else{
    		$data['id'] = $id;
    		$data['status'] = 1;
    		$res = $db->save($data);
    		//$db->where("id='{$id}'")->setField('status',1);
    		$log = "将快递公司名为".$res['name']."的状态从启用变成禁用";
    	}
    	$this->add_log($log);
    	showmsg(200,'操作成功');
    }
    /**
     * 删除快递的状态
     */
    public function del_exp(){
    	$id = I('id');
    	if($id){
    		$db = M('express_list');
    		$id = explode(",", $id);
    		foreach ($id as $v){
    			$re = $db->where("id='{$v}'")->getField('name');
    			$db->where("id='{$v}'")->delete();
    			$log = "删除了快递公司名为".$re;
    			$this->add_log($log);
    		}
    		showmsg(200,'操作成功');
    	}else{
    		showmsg(300,'请选择你要删除的快递公司');
    	}
    }
    /*
     * 设定格价组
    * default  销售价,
    * vids  规格组id
    * */
    public function member_price() {
    	$def = I('get.default');
    	$vid = I('get.vids');
    	$product_id = I('get.product_id/d');
    	if($product_id){
    		$deff = M('group_price as gp')->join("".C('DB_PREFIX')."user_group as ug on ug.id = gp.group_id")->where("gp.product_id='{$product_id}'")->field('gp.price,ug.discount')->find();
    		if(!empty($deff)){
    			$def = $deff['price']/$deff['discount']*100;
    		}
    	}
    	$this->assign('def', $def);
    	$this->assign('vid', $vid);
    	$group = M('user_group')->order("id desc")->field("id,name,discount")->select();
    	$this->assign('group',$group);
    	$this->display();
    }
    /**
     * 规格图片
     */
    public function spec_upload(){
    	$product_id = I('get.product_id/d');
    	if($product_id){
    		$photo = M('products')->where("id='{$product_id}'")->getField('photo_list');
    		if(!empty($photo)){
    			$photolist = explode(",",$photo);
    			$this->assign('photolist',$photolist);
    		}
    	}
    	$this->display();
    }
    public function firend(){
    	$id = I('id/d');
    	if($id){
    		$map['id'] = $id;
    		$list = lists('friend_link','',$map,"*",'',0,0,1);
    		$this->assign('va',$list);
    	}
    	$this->display();
    }
    /**
     * 保存友情链接
     */
    public function save_friend(){
    	$db = D('Common/FriendLink');
    	if($create = $db->create()){
    		$id = I('id/d');
    		if($id){
    			$create['id'] = $id;
    			$res = $db->save($create);
    		}else{
    			$log = "添加了一个新的链接,名称为".$create['name'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			
    			showmsg(200,'保存编辑成功');
    		}else{
    			showmsg(300,'你没有做任何的修改，无需保存编辑');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 改变友情链接的打开的方式
     */
    public function update_friend_blank(){
    	$id = I('id/d');
    	$db = M('friend_link');
    	$res = $db->where("id='{$id}'")->field("name,is_blank")->find();
    	if($res['is_blank'] == 1){
    		$db->where("id='{$id}'")->setField('is_blank',0);
    		$log = "将友情链接名为".$res['name']."打开方式有新窗口变成当前窗口打开";
    	}else{
    		$db->where("id='{$id}'")->setField('is_blank',1);
    		$log = "将友情链接名为".$res['name']."打开方式有当前窗口变成新窗口打开";
    	}
    	$this->add_log($log);
    	showmsg(200,'修改成功');
    }
    /**
     * 改变友情链接的显示方式
     */
    public function update_friend_status(){
    	$id = I('id/d');
    	$db = M('friend_link');
    	$res = $db->where("id='{$id}'")->field("name,status")->find();
    	if($res['status'] == 1){
    		$db->where("id='{$id}'")->setField('status',0);
    		$log = "将友情链接名为".$res['name']."的状态有关闭变成开启";
    	}else{
    		$db->where("id='{$id}'")->setField('status',1);
    		$log = "将友情链接名为".$res['name']."的状态有开启变成关闭";
    	}
    	$this->add_log($log);
    	showmsg(200,'修改成功');
    }
    /**
     * 删除友情链接
     */
    public function del_friend(){
    	$id = I('id/d');
    	$db = M('friend_link');
    	$res = $db->where("id='{$id}'")->field("name,status")->find();
    	$log = "删除了一个友情链接，名为：".$res['name'];
    	$db->where("id='{$id}'")->delete();
    	$this->add_log($log);
    	showmsg(200,'删除成功');
    }
    /**
     * 添加、编辑导航栏
     */ 
    public function nav(){
    	$id = I('id/d');
    	if($id){
    		$map['id'] = $id;
    		$list = lists('nav','',$map,"*",'',0,0,1);
    		$this->assign('va',$list);
    	}
    	$where['parent_id'] = 0;
    	$res = lists('nav','',$where,'*','',0,0,2);
    	$this->assign('res',$res);
    	$this->display();
    }
    /**
     * 保存导航栏
     */
    public function save_nav(){
    	$db = D('Common/Nav');
    	if($create = $db->create()){
    		$id = I('id/d');
    		if($create['parent_id']){
    			$create['leve'] = 2;
    		}else{
    			$create['leve'] = 1;
    		}
    		if($id){
    			$create['id'] = $id;
    			$res = $db->save($create);
    		}else{
    			$log = "添加了一个新的导航栏,名称为".$create['name'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			showmsg(200,'保存编辑成功');
    		}else{
    			showmsg(300,'你没有做任何的修改，无需保存编辑');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
     /**
     * 改变导航栏的打开的方式
     */
    public function update_nav_blank(){
    	$id = I('id/d');
    	$db = M('nav');
    	$res = $db->where("id='{$id}'")->field("name,is_blank")->find();
    	if($res['is_blank'] == 1){
    		$db->where("id='{$id}'")->setField('is_blank',0);
    		$log = "将导航栏名为".$res['name']."打开方式有新窗口变成当前窗口打开";
    	}else{
    		$db->where("id='{$id}'")->setField('is_blank',1);
    		$log = "将导航栏名为".$res['name']."打开方式有当前窗口变成新窗口打开";
    	}
    	$this->add_log($log);
    	showmsg(200,'修改成功');
    }
    /**
     * 改变友情链接的显示方式
     */
    public function update_nav_status(){
    	$id = I('id/d');
    	$db = M('nav');
    	$res = $db->where("id='{$id}'")->field("name,status")->find();
    	if($res['status'] == 1){
    		$db->where("id='{$id}'")->setField('status',0);
    		$log = "将导航栏名为".$res['name']."的状态有关闭变成开启";
    	}else{
    		$db->where("id='{$id}'")->setField('status',1);
    		$log = "将导航栏名为".$res['name']."的状态有开启变成关闭";
    	}
    	$this->add_log($log);
    	showmsg(200,'修改成功');
    }
    /**
     * 删除友情链接
     */
    public function del_nav(){
    	$id = I('id/d');
    	$db = M('nav');
    	$res = $db->where("id='{$id}'")->field("name,status")->find();
    	$log = "删除了一个导航栏，名为：".$res['name'];
    	$db->where("id='{$id}'")->delete();
    	$this->add_log($log);
    	showmsg(200,'删除成功');
    }
    /**
     * 添加、编辑区域
     */
    public function area(){
    	$id = I('id/d');
    	$db = M('region');
    	if($id){
    		
    		$list = $db->where("id='{$id}'")->find();
    		if($list['level'] == 3){
    			$list['pre_parent_id'] = $list['parent_id'];
    			$parent_name = $db->where("id='{$list['parent_id']}'")->field('parent_id,name')->find();
    			$list['parent_name'] = $parent_name['name'];
    			$list['parent_id'] = $parent_name['parent_id'];
    		}
    		if($list['level'] == 4){
    			$list['pre_pre_parent_id'] = $list['parent_id'];
    			$parent_name = $db->where("id='{$list['parent_id']}'")->field('parent_id,name')->find();
    			$list['pre_parent_id'] = $parent_name['parent_id'];
    			$list['pre_parent_name'] = $parent_name['name'];
    			$re_parent_name = $db->where("id='{$parent_name['parent_id']}'")->find();
    			$list['parent_name'] = $re_parent_name['name'];
    			$list['parent_id'] = $re_parent_name['parent_id'];
    		}
    		$this->assign('va',$list);
    	}
    	$res = $db->where("parent_id='0'")->order("id asc")->select();
    	$this->assign('res',$res);
    	$this->display();
    }
    /**
     * 保存区域
     */
    public function save_area(){
    	$db = D('Region');
    	if($create = $db->create()){
    		$id = I('id/d');
    		$parent_id = I('parent_id');
    		$pre_parent_id = I('pre_parent_id');
    		$pre_pre_parent_id = I('pre_pre_parent_id');
    		if(!empty($pre_pre_parent_id)){
    			$create['parent_id'] = $pre_pre_parent_id;
    			$create['level'] = 4;
    		}elseif (empty($pre_pre_parent_id) && !empty($pre_parent_id)){
    			$create['parent_id'] = $pre_parent_id;
    			$create['level'] = 3;
    		}elseif(empty($pre_parent_id) && !empty($parent_id)){
    			$create['parent_id'] = $parent_id;
    			$create['level'] = 2;
    		}else {
    			$create['level'] = 1;
    		}
    		if($id){
    			$create['id'] = $id;
    			$res = $db->save($create);
    		}else{
    			$log = "新增了一个区域名为".$create['name'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			showmsg(200,'保存成功');
    		}else{
    			showmsg(300,'你没有做任何的修改 ，无需保存编辑');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 删除区域
     */
    public function del_area(){
    	$id = I('id/d');
    	$db = M('Region');
    	$res = $db->where("id='{$id}'")->field("id,name,status,parent_id")->find();
    	$re = $db->where("parent_id='{$res['id']}'")->find();
    	if(!empty($re)){
    		showmsg(300,'你还有下级区域在使用，无法删除');exit();
    	}else{
    		$log = "删除了一个区域，名为：".$res['name'];
    		$db->where("id='{$id}'")->delete();
    		$this->add_log($log);
    		showmsg(200,'删除成功');
    	}
    }
    /**
     * 改变区域的状态
     */
    public function update_area_status(){
    	$id = I('id/d');
    	$db = M('Region');
    	$res = $db->where("id='{$id}'")->field("id,level,name,status")->find();
    	if($res['status'] == 1){
    		//$db->where("parent_id='{$id}'")->setField('status',0);
    		$db->where("id='{$id}'")->setField('status',0);
    		$this->update_next_area($id,$res['level'],0);
    		$log = "将区域名为".$res['name']."的状态有关闭变成开启";
    	}else{
    		//$db->where("parent_id='{$id}'")->setField('status',1);
    		$db->where("id='{$id}'")->setField('status',1);
    		$this->update_next_area($id,$res['level'],1);
    		$log = "将区域名为".$res['name']."的状态有开启变成关闭";
    	}
    	$this->add_log($log);
    	showmsg(200,'修改成功');
    }
    /**
     * 添加、编辑城市
     */
    public function city(){
    	$id = I('id/d');
    	$db = M('city');
    	if($id){
    
    		$list = $db->where("id='{$id}'")->find();
    		if($list['level'] == 3){
    			$list['pre_parent_id'] = $list['parent_id'];
    			$parent_name = $db->where("id='{$list['parent_id']}'")->field('parent_id,name')->find();
    			$list['parent_name'] = $parent_name['name'];
    			$list['parent_id'] = $parent_name['parent_id'];
    		}
    		if($list['level'] == 4){
    			$list['pre_pre_parent_id'] = $list['parent_id'];
    			$parent_name = $db->where("id='{$list['parent_id']}'")->field('parent_id,name')->find();
    			$list['pre_parent_id'] = $parent_name['parent_id'];
    			$list['pre_parent_name'] = $parent_name['name'];
    			$re_parent_name = $db->where("id='{$parent_name['parent_id']}'")->find();
    			$list['parent_name'] = $re_parent_name['name'];
    			$list['parent_id'] = $re_parent_name['parent_id'];
    		}
    		$this->assign('va',$list);
    	}
    	$res = $db->where("parent_id='0'")->order("id asc")->select();
    	$this->assign('res',$res);
    	$this->display();
    }
    /**
     * 保存城市
     */
    public function save_city(){
    	$db = D('City');
    	if($create = $db->create()){
    		$id = I('id/d');
    		$parent_id = I('parent_id');
    		$pre_parent_id = I('pre_parent_id');
    		$pre_pre_parent_id = I('pre_pre_parent_id');
    		if(!empty($pre_pre_parent_id)){
    			$create['parent_id'] = $pre_pre_parent_id;
    			$create['level'] = 4;
    		}elseif (empty($pre_pre_parent_id) && !empty($pre_parent_id)){
    			$create['parent_id'] = $pre_parent_id;
    			$create['level'] = 3;
    		}elseif(empty($pre_parent_id) && !empty($parent_id)){
    			$create['parent_id'] = $parent_id;
    			$create['level'] = 2;
    		}else {
    			$create['level'] = 1;
    		}
    		if($id){
    			$create['id'] = $id;
    			$res = $db->save($create);
    		}else{
    			$log = "新增了一个城市名为".$create['name'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			showmsg(200,'保存成功');
    		}else{
    			showmsg(300,'你没有做任何的修改 ，无需保存编辑');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 删除区域
     */
    public function del_city(){
    	$id = I('id/d');
    	$db = M('City');
    	$res = $db->where("id='{$id}'")->field("id,name,status,parent_id")->find();
    	$re = $db->where("parent_id='{$res['id']}'")->find();
    	if(!empty($re)){
    		showmsg(300,'你还有下级区域在使用，无法删除');exit();
    	}else{
    		$log = "删除了一个区域，名为：".$res['name'];
    		$db->where("id='{$id}'")->delete();
    		$this->add_log($log);
    		showmsg(200,'删除成功');
    	}
    }
    /**
     * 改变城市的状态
     */
    public function update_city_status(){
    	$id = I('id/d');
    	$db = M('City');
    	$res = $db->where("id='{$id}'")->field("id,level,name,status")->find();
    	if($res['status'] == 1){
    		//$db->where("parent_id='{$id}'")->setField('status',0);
    		$db->where("id='{$id}'")->setField('status',0);
    		$this->update_next_area($id,$res['level'],0);
    		$log = "将城市名为".$res['name']."的状态有关闭变成开启";
    	}else{
    		//$db->where("parent_id='{$id}'")->setField('status',1);
    		$db->where("id='{$id}'")->setField('status',1);
    		$this->update_next_area($id,$res['level'],1);
    		$log = "将城市名为".$res['name']."的状态有开启变成关闭";
    	}
    	$this->add_log($log);
    	showmsg(200,'修改成功');
    }
    /**
     * 添加、编辑配送方式
     */
    public  function freights(){
    	$id = I('id/d');
    	if($id){
    		$list = M('delivery')->where("id='{$id}'")->find();
    		if(!empty($list)){
    			$list['area_groupid'] = unserialize($list['area_groupid']);
    			foreach ($list['area_groupid'] as $i=>$va){
    				$newname[$i] = explode(";",$va);
    				array_pop($newname[$i]);
    				array_shift($newname[$i]);
    				$name[$i] = area_name($newname[$i]);
    				$list['area_name'][$i] = join(",",$name[$i]);
    			}
    			$list['firstprice'] = unserialize($list['firstprice']);
    			$list['secondprice'] = unserialize($list['secondprice']);
    			$this->assign('va',$list);
    		}else{
    			showmsg(300,'错误');exit();
    		}
    	}
    	$res = M('Region')->where("parent_id='0' and status='0'")->order("id asc")->select();
    	$this->assign('res',$res);
    	$this->display();
    }
    /**
     * 保存配送方式
     */
    public function save_freights(){
    	$db = D('Delivery');
    	if($create = $db->create()){
    		if(empty($create['open_default'])){
    			$create['open_default'] = 0;
    		}
    		//print_r($create);exit();
    		$id = I('id');
    		//支持的配送地区ID
    		$area_groupid = $_POST['area_groupid'];
    		//配送地址对应的首重价格
    		$firstprice = $_POST['firstprice'];
    		//配送地区对应的续重价格
    		$secondprice = $_POST['secondprice'];
    		if($firstprice){
    			$create['firstprice'] = serialize($firstprice);
    		}
    		if($secondprice){
    			$create['secondprice'] = serialize($secondprice);
    		}
    		if($area_groupid){
    			$create['area_groupid'] = serialize($area_groupid);
    		}
    		if($id){
    			$create['id'] = $id;
    			$res = $db->save($create);
    		}else{
    			$log = "新增了一个配送方式名为".$create['name'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			showmsg(200,'保存成功');
    		}else{
    			showmsg(300,'保存失败');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 改变配送方式的状态
     */
    public function update_freights(){
    	$id = I('id/d');
    	$db = M('Delivery');
    	$res = $db->where("id='{$id}'")->field("id,name,status")->find();
    	if($res['status'] == 1){
    		//$db->where("parent_id='{$id}'")->setField('status',0);
    		$db->where("id='{$id}'")->setField('status',0);
    		$log = "将配送方式名为".$res['name']."的状态有关闭变成开启";
    	}else{
    		//$db->where("parent_id='{$id}'")->setField('status',1);
    		$db->where("id='{$id}'")->setField('status',1);
    		$log = "将配送方式名为".$res['name']."的状态有开启变成关闭";
    	}
    	$this->add_log($log);
    	showmsg(200,'修改成功');
    }
    /**
     * 删除区域
     */
    public function del_freights(){
    	$id = I('id/d');
    	$db = M('Delivery');
    	$res = $db->where("id='{$id}'")->field("name,status")->find();
    	$log = "删除了一个配送方式，名为：".$res['name'];
    	$db->where("id='{$id}'")->delete();
    	$this->add_log($log);
    	showmsg(200,'删除成功');	
    }
    /**
     * 增加、编辑角色
     */
    public function role(){
    	$id = I('id/d');
    	if($id){
    		$map['id'] = $id;
    		$list = lists('role','',$map,"*",'',0,0,1);
    		$this->assign('va',$list);
    	}
    	$this->display();
    }
    /**
     * 保存角色
     */
    public function save_role(){
    	$db = D('Role');
    	if($create =  $db->create()){
    		$id = I('id/d');
    		if($id){
    			$create['id'] = $id;
    			$res = $db->save($create);
    		}else{
    			$log = "添加了一个新的角色,名称为".$create['name'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		showmsg(200,'保存编辑成功');
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 删除角色
     */
    public function del_role(){
    	$id = I('id/d');
    	$db = M('role');
    	$res = $db->where("id='{$id}'")->field("name")->find();
    	$log = "删除了一个角色，名为：".$res['name'];
    	$db->where("id='{$id}'")->delete();
    	$this->add_log($log);
    	showmsg(200,'删除成功');
    }
    /**
     * 增加、编辑管理员
     */
    public function admin(){
    	$id = I('id/d');
    	if($id){
    		$map['id'] = $id;
    		$list = lists('admin','',$map,"*",'',0,0,1);
    		$this->assign('va',$list);
    	}
    	//角色列表
    	$res = lists('role','','','*','',0,0,2);
    	$this->assign('res',$res);
    	$this->display();
    }
    /**
     * 保存角色
     */
    public function save_admin(){
    	$db = D('Admin');
    	if($create =  $db->create()){
    		$log = "添加了一个新的管理员,名称为".$create['username'];
    		$this->add_log($log);
    		$res = $db->add($create);
    		showmsg(200,'保存编辑成功');
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 删除管理员
     */
    public function del_admin(){
    	$id = I('id/d');
    	$db = M('admin');
    	$res = $db->where("id='{$id}'")->field("username")->find();
    	$log = "删除了一个管理员，名为：".$res['username'];
    	$db->where("id='{$id}'")->delete();
    	$this->add_log($log);
    	showmsg(200,'删除成功');
    }
    /**
     * 改变管理员的状态
     */
    public function update_admin(){
    	$id = I('id/d');
    	$db = M('admin');
    	$res = $db->where("id='{$id}'")->field("username,status")->find();
    	if($res['status'] == 1){
    		$db->where("id='{$id}'")->setField('status',0);
    		$log = "将管理员名为".$res['username']."的状态有关闭变成开启";
    	}else{
    		$db->where("id='{$id}'")->setField('status',1);
    		$log = "将管理员为".$res['username']."的状态有开启变成关闭";
    	}
    	$this->add_log($log);
    	showmsg(200,'修改成功');
    }
    /**
     * 重置管理员的密码
     */
    public function reset_admin(){
    	$id = I('id/d');
    	$db = D('Admin');
    	$re = $db->where("id='{$id}'")->field("username")->find();
    	$basc = F('web_web');
    	$newpasswor = $basc['admin_password']?$basc['admin_password']:'123456';
    	$newpassword = $db->autopassword($newpasswor);
    	$res = $db->where("id='{$id}'")->setField('password',$newpassword);
    	if($res!=false){
    		$log = "重置了管理员名为".$re['username']."的密码，新密码为".$newpasswor;
    		$this->add_log($log);
    		showmsg(200,'重置成功，新密码为'.$newpasswor);
    	}else{
    		showmsg(300,'重置失败');
    	}
    }
    /**
     * 添加、编辑文章
     */
    public function send_art(){
    	$id = I('id/d');
    	if($id){
    		$map['id'] = $id;
    		$list = lists('article','',$map,"*",'',0,0,1);
    		$this->assign('va',$list);
    	}
    	//文章分类列表
    	$where['status'] = 0;
    	$where['parent_id'] = 0;
    	$res = lists('class_article','',$where,'*','id desc',0,0,2);
    	$this->assign('res',$res);
    	$this->display();
    }
    /**
     * 保存文章
     */
    public function save_article(){
    	$db = D('Article');
    	if($create = $db->create()){
    		$create['content'] = $_POST['content'];
    		$id = I('id/d');
    		if($id){
    			$create['id'] = $id;
    			$res = $db->save($create);
    		}else{
    			$log = "新增了一篇文章名为".$create['title'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			showmsg(200,'保存成功');
    		}else{
    			showmsg(300,'你没有做任何的修改 ，无需保存编辑');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 删除文章
     */
    public function del_article(){
    	$id = I('id');
    	$idarray = explode(",",$id);
    	$db = M('article');
    	foreach ($idarray as $va){
    		$re = $db->where("id='{$va}'")->field("title")->find();
    		$log = "删除了一篇文章，名为".$re['title'];
    		$this->add_log($log);
    		$db->where("id='{$va}'")->delete();
    	}
    	showmsg(200,'删除成功');
    }
    /**
     * 添加、编辑文章分类
     */
    public function art_type(){
    	$id = I('id/d');
    	if($id){
    		$map['id'] = $id;
    		$list = lists('class_article','',$map,"*",'',0,0,1);
    		$this->assign('va',$list);
    	}
    	$where['status'] = 0;
    	$where['parent_id'] = 0;
    	$res = lists('class_article','',$where,'*','id desc',0,0,2);
    	$this->assign('res',$res);
    	$this->display();
    }
    /**
     * 保存文章分类
     */
    public function save_class_article(){
    	$db = D('ClassArticle');
    	if($create = $db->create()){
    		$id = I('id/d');
    		if($create['parent_id']){
    			$create['leve'] = 2;
    		}else{
    			$create['leve'] = 1;
    		}
    		if($id){
    			$create['id'] = $id;
    			$res = $db->save($create);
    		}else{
    			$log = "新增了一个文章分类名为".$create['name'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			showmsg(200,'保存成功');
    		}else{
    			showmsg(300,'你没有做任何的修改 ，无需保存编辑');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 改变文章分类的状态
     */
    public function update_class_article(){
    	$id = I('id/d');
    	$db = M('class_article');
    	$res = $db->where("id='{$id}'")->field("name,status")->find();
    	if($res['status'] == 1){
    		$db->where("id='{$id}'")->setField('status',0);
    		$log = "将文章分类名为".$res['name']."的状态由关闭变成开启";
    	}else{
    		$db->where("id='{$id}'")->setField('status',1);
    		$log = "将文章分类名为".$res['name']."的状态由开启变成关闭";
    	}
    	$this->add_log($log);
    	showmsg(200,'修改成功');
    }
    /**
     * 删除文章分类
     */
    public function del_class_article(){
    	$id = I('id/d');
    	$db = M('class_article');
    	$res = $db->where("id='{$id}'")->field("name,status")->find();
    	$log = "删除了一个文章分类，名为：".$res['name'];
    	if($res['parent_id'] == 0){
    		$db->where("parent_id ='{$res['id']}'")->delete();
    	}
    	$db->where("id='{$id}'")->delete();
    	$this->add_log($log);
    	showmsg(200,'删除成功');
    }
    /**
     * 添加、编辑专题
     */
    public function sub(){
    	$id = I('id/d');
    	if($id){
    		$map['id'] = $id;
    		$list = lists('subject','',$map,"*",'',0,0,1);
    		$this->assign('va',$list);
    	}
    	$this->display();
    }
    /**
     * 保存专题
     */
    public function save_subject(){
    	$db = D('Subject');
    	if($create = $db->create()){
    		$create['content'] = $_POST['content'];
    		$id = I('id/d');
    		if($id){
    			$create['id'] = $id;
    			$res = $db->save($create);
    		}else{
    			$log = "新增了一篇专题名为".$create['title'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			showmsg(200,'保存成功');
    		}else{
    			showmsg(300,'你没有做任何的修改 ，无需保存编辑');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 删除专题
     */
    public function del_subject(){
    	$id = I('id');
    	$idarray = explode(",",$id);
    	$db = M('subject');
    	foreach ($idarray as $va){
    		$re = $db->where("id='{$va}'")->field("title")->find();
    		$log = "删除了一篇专题，名为".$re['title'];
    		$this->add_log($log);
    		$db->where("id='{$va}'")->delete();
    	}
    	showmsg(200,'删除成功');
    }
    /**
     * 添加、编辑广告位置
     */
    public function seat(){
    	$id = I('id/d');
    	if($id){
    		$map['id'] = $id;
    		$list = lists('ad_place','',$map,"*",'',0,0,1);
    		$this->assign('va',$list);
    	}
    	$this->display();
    }
    /**
     * 保存广告位置
     */
    public function save_ad_place(){
    	$db = D('AdPlace');
    	if($create = $db->create()){
    		$id = I('id/d');
    		if($id){
    			$create['id'] = $id;
    			$res = $db->save($create);
    		}else{
    			$log = "新增了一个广告位置名为".$create['name'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			showmsg(200,'保存成功');
    		}else{
    			showmsg(300,'你没有做任何的修改 ，无需保存编辑');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 改变广告位置的状态
     */
    public function update_ad_place(){
    	$id = I('id/d');
    	$db = M('ad_place');
    	$res = $db->where("id='{$id}'")->field("name,status")->find();
    	if($res['status'] == 1){
    		$db->where("id='{$id}'")->setField('status',0);
    		$log = "将广告位置名为".$res['name']."的状态有关闭变成开启";
    	}else{
    		$db->where("id='{$id}'")->setField('status',1);
    		$log = "将广告位置为".$res['name']."的状态有开启变成关闭";
    	}
    	$this->add_log($log);
    	showmsg(200,'修改成功');
    }
    /**
     * 删除广告位置
     */
    public function del_ad_place(){
    	$id = I('id/d');
    	$db = M('ad_place');
    	$res = $db->where("id='{$id}'")->field("name,status")->find();
    	$log = "删除了一个广告位置，名为：".$res['name'];
    	$db->where("id='{$id}'")->delete();
    	$this->add_log($log);
    	showmsg(200,'删除成功');
    }
    /**
     * 添加、编辑广告
     */
    public function adv(){
    	$id = I('id/d');
    	if($id){
    		$map['id'] = $id;
    		$list = lists('ad','',$map,"*",'',0,0,1);
    		$this->assign('va',$list);
    	}
    	//分类
    	$res = M('ad_place')->where("status='0'")->order("sort desc")->select();
    	$this->assign('res',$res);
    	$this->display();
    }
    /**
     * 保存广告位置
     */
    public function save_ad(){
    	$db = D('Ad');
    	if($create = $db->create()){
    		$id = I('id/d');
    		if($id){
    			$create['id'] = $id;
    			$res = $db->save($create);
    		}else{
    			$log = "新增了一个广告 名为".$create['name'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			showmsg(200,'保存成功');
    		}else{
    			showmsg(300,'你没有做任何的修改 ，无需保存编辑');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 改变广告的状态
     */
    public function update_ad(){
    	$id = I('id/d');
    	$db = M('ad');
    	$res = $db->where("id='{$id}'")->field("name,status")->find();
    	if($res['status'] == 1){
    		$db->where("id='{$id}'")->setField('status',0);
    		$log = "将广告名为".$res['name']."的状态有关闭变成开启";
    	}else{
    		$db->where("id='{$id}'")->setField('status',1);
    		$log = "将广告为".$res['name']."的状态有开启变成关闭";
    	}
    	$this->add_log($log);
    	showmsg(200,'修改成功');
    }
    /**
     * 删除广告
     */
    public function del_ad(){
    	$id = I('id');
    	$idarray = explode(",",$id);
    	$db = M('ad');
    	foreach ($idarray as $va){
    		$re = $db->where("id='{$va}'")->field("name,photo")->find();
    		unlink(".".$re['photo']);
    		$log = "删除了一个广告，名为".$re['name'];
    		$this->add_log($log);
    		$db->where("id='{$va}'")->delete();
    	}
    	showmsg(200,'删除成功');
    }
    /**
     * 查看、编辑会员等级
     */
    public function lev(){
    	$id = I('id/d');
    	if($id){
    		$map['id'] = $id;
    		$list = lists('user_group','',$map,"*",'',0,0,1);
    		$this->assign('va',$list);
    	}
    	$this->display();
    }
    /**
     * 保存会员组
     */
    public function save_user_leve(){
    	$db = D('UserGroup');
    	if($create = $db->create()){
    		$id = I('id/d');
    		if($id){
    			$create['id'] = $id;
    			$res = $db->save($create);
    		}else{
    			$log = "新增了一个会员组 名为".$create['name'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			showmsg(200,'保存成功');
    		}else{
    			showmsg(300,'你没有做任何的修改 ，无需保存编辑');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 添加、编辑会员
     */
    public function addmember(){
    	$id = I('id/d');
    	if($id){
    		$map['id'] = $id;
    		$list = lists('user','',$map,"*",'',0,0,1);
    		$this->assign('va',$list);
    	}
    	$this->display();
    }
    /**
     * 保存会员
     */
    public function save_user(){
    	$db = D('Common/User');
    	if($create = $db->create()){
    		$id = I('id/d');
    		if($id){
    			$create['id'] = $id;
    			$res = $db->save($create);
    		}else{
    			$log = "新增了一个会员 手机号为".$create['mobile'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			showmsg(200,'保存成功');
    		}else{
    			showmsg(300,'你没有做任何的修改 ，无需保存编辑');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 添加商家
     */
    public function addshop(){
    	$id = I('id/d');
    	if($id){
    		$map['id'] = $id;
    		$list = lists('shop','',$map,"*",'',0,0,1);
    		$this->assign('va',$list);
    	}
    	$res = M('region')->where("parent_id='0'")->select();
    	$this->assign('res',$res);
    	//商家分类
    	$class = M('ShopClassify')->where("status='0'")->order("sort desc")->select();
    	$this->assign('class',$class);
    	$this->display();
    }
    
    /**
     * 保存商家
     */
    public function save_shop(){
    	$db = D('Common/Shop');
    	if($create = $db->create()){
    		list($create['east'],$create['north']) = explode(",",$create['east']);
    		if(empty($create['north'])){
    			showmsg(300,'经纬度填写错误');exit();
    		}
    		$id = I('id');
    		$body = $_POST['body'];
    		if($id){
    			$create['id'] = $id;
    			$res = $db->save($create);
    		}else{
    			$log = "新增了一个商家  商家手机号为".$create['username'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			showmsg(200,'保存成功');
    		}else{
    			showmsg(300,'保存失败');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 删除商家
     */
    public function del_shop(){
    	$id = I('id/d');
    	$name = M('shop')->where("id='{$id}'")->field("shop_name")->find();
    	M('shop')->where("id='{$id}'")->delete();
    	$log = "删除了一个商家，商家的名称为".$name['shop_name'];
    	$this->add_log($log);
    	showmsg(200,'删除成功');exit();
    }
    /**
     * 改变商家
     */
    public function update_shop(){
    	$id = I('id/d');
    	$name = M('shop')->where("id='{$id}'")->field("shop_name,close")->find();
    	if($name['close']){
    		M('shop')->where("id='{$id}'")->setField('close',0);
    		$log = "开启了一个商家，商家的名称为".$name['shop_name'];
    	}else{
    		M('shop')->where("id='{$id}'")->setField('close',1);
    		$log = "关闭了一个商家，商家的名称为".$name['shop_name'];
    	}
    	$this->add_log($log);
    	showmsg(200,'删除成功');exit();
    }
    /**
     * 审核商家
     */
    public function checkshop(){
    	$id = I('id/d');
    	if($id){
    		$map['id'] = $id;
    		$list = lists('shop','',$map,"*",'',0,0,1);
    		$this->assign('va',$list);
    	}
    	$this->display();
    }
    /**
     * 审核商家
     */
    public function save_check_shop(){
    	$is_check = I('is_check/d');
    	$check_reason = I('check_reason');
    	if($is_check == 2){
    		if(empty($check_reason)){
    			showmsg(300,'请输入审核不通过的原因');exit();
    		}
    	}
    	$data['id'] = I('id');
    	$data['check_id'] = session('bxd_shopping_admin_id');
    	$data['check_reason'] = $check_reason;
    	$data['is_check'] = $is_check;
    	if($is_check == 2){
    		$log = "审核不通过了一个商家";
    	}else{
    		$log = "审核通过了一个商家";
    	}
    	$this->add_log($log);
    	M('shop')->save($data);
    	showmsg(200,'操作成功');
    }
    /**
     * 添加、编辑商家分类
     */
    public function shoplev(){
    	$id = I('id/d');
    	if($id){
    		$list = M('ShopClassify')->where("id='{$id}'")->find();
    		$this->assign('va',$list);
    	}
    	$this->display();
    }
    /**
     * 保存商家分类
     */
    public function save_shop_leve(){
    	$db = D('Common/ShopClassify');
    	if($create = $db->create()){
    		$id = I('id');
    		$body = $_POST['body'];
    		if($id){
    			$create['id'] = $id;
    			$res = $db->save($create);
    		}else{
    			$log = "新增了一个商家分类  分类名为".$create['name'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			showmsg(200,'保存成功');
    		}else{
    			showmsg(300,'保存失败');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 删除商家分类
     */
    public function del_shop_lev(){
    	$id = I('get.id');
    	if($id){
    		$name = M('ShopClassify')->where("id='{$id}'")->field("name")->find();
    		M('ShopClassify')->where("id='{$id}'")->delete();
    		$log = "删除了一个商家分类，分类的名称为".$name['name'];
    		$this->add_log($log);
    		showmsg(200,'删除成功');exit();
    	}else{
    		showmsg(300,'删除失败');
    	} 
    }
    /**
     * 添加、编辑规格
     */
    public function spec(){
    	$id = I('id/d');
    	if($id){
    		$map['id'] = $id;
    		$list = lists('spec','',$map,"*",'',0,0,1);
    		$value = explode(",",$list['value']);
    		$list['value'] = json_encode($value);
    		$this->assign('va',$list);
    	}
    	$this->display();
    }
    /**
     * 保存规格
     */
    public function save_spec(){
    	$db = D('spec');
    	if($create = $db->create()){
    		$value = $_POST['value'];
    		if(empty($value)){
    			showmsg(300,'请输入规格值');exit();
    		} 
    		$create['value'] = join(",",$value);
    		$id = I('id/d');
    		if($id){
    			$create['id'] = $id;
    			$res = $db->save($create);
    		}else{
    			$log = "新增了一个规格 规格号为".$create['name'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			showmsg(200,'保存成功');
    		}else{
    			showmsg(300,'你没有做任何的修改 ，无需保存编辑');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 删除规格
     */
    public function del_spec(){
    	$id = I('id');
    	$idarray = explode(",",$id);
    	$db = M('spec');
    	foreach ($idarray as $va){
    		$re = $db->where("id='{$va}'")->field("name")->find();
    		$log = "删除了一个规格，名为".$re['name'];
    		$this->add_log($log);
    		$db->where("id='{$va}'")->setField('is_del',1);
    	}
    	showmsg(200,'删除成功');
    }
    /**
     * 添加、编辑属性
     */
    public function attr(){
    	$id = I('id/d');
    	if($id){
    		$map['id'] = $id;
    		$list = lists('attribute','',$map,"*",'',0,0,1);
    		if(!empty($list['value'])){
    			$value = explode(",",$list['value']);
    			$list['value'] = json_encode($value);
    		}
    		$this->assign('va',$list);
    	}
    	$this->display();
    }
    /**
     * 保存规格
     */
    public function save_attr(){
    	$db = D('Attribute');
    	if($create = $db->create()){
    		$value = $_POST['value'];
    		if(!empty($value)){
    			$create['value'] = join(",",$value);
    		}
    		$id = I('id/d');
    		if($id){
    			$create['id'] = $id;
    			$res = $db->save($create);
    		}else{
    			$log = "新增了一个属性 属性名为".$create['name'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			showmsg(200,'保存成功');
    		}else{
    			showmsg(300,'你没有做任何的修改 ，无需保存编辑');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 删除规格
     */
    public function del_attr(){
    	$id = I('id');
    	$idarray = explode(",",$id);
    	$db = M('attribute');
    	foreach ($idarray as $va){
    		$re = $db->where("id='{$va}'")->field("name")->find();
    		$log = "删除了一个属性，名为".$re['name'];
    		$this->add_log($log);
    		$db->where("id='{$va}'")->setField('is_del',1);
    	}
    	showmsg(200,'删除成功');
    }
    /**
     * 添加、编辑模型
     */
    public function types(){
    	$id = I('id/d');
    	if($id){
    		$map['id'] = $id;
    		$list = lists('model','',$map,"*",'',0,0,1);
    		if(!empty($list['attr_ids'])){
    			$data['id'] = array('in',$list['attr_ids']);
    			$order = "find_in_set(id,'".$list['attr_ids']."')";
    			$list['attr'] = lists('attribute','',$data,"id,name,value,type",$order, 0,0, 2);
    		}
    		if(!empty($list['spec_ids'])){
    			$data['id'] = array('in',$list['spec_ids']);
    			$order = "find_in_set(id,'".$list['spec_ids']."')";
    			$list['spec'] = lists('spec','',$data,"id,name,value",$order, 0,0, 2);
    		}
    		$this->assign('va',$list);
    	}
    	$this->display();
    }
    /**
     * 保存模型
     */
    public function save_model(){
    	$db = D('Model');
    	if($create = $db->create()){
    		$id = I('mid/d');
    		if($id){
    			$create['id'] = $id;
    			$res = $db->save($create);
    		}else{
    			$log = "新增了一个模型 模型名为".$create['name'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			showmsg(200,'保存成功');
    		}else{
    			showmsg(300,'你没有做任何的修改 ，无需保存编辑');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 删除模型
     */
    public function del_model(){
    	$id = I('id');
    	$idarray = explode(",",$id);
    	$db = M('model');
    	foreach ($idarray as $va){
    		$re = $db->where("id='{$va}'")->field("name")->find();
    		$log = "删除了一个模型，名为".$re['name'];
    		$this->add_log($log);
    		$db->where("id='{$va}'")->setField('is_del',1);
    	}
    	showmsg(200,'删除成功');
    }
    /**
     * 添加、编辑商品分类
     */
    public function groy(){
    	$id = I('id/d');
    	if($id){
    		$map['id'] = $id;
    		$list = lists('category','',$map,"*",'',0,0,1);
    		if($list['leve'] == 3){
    			$parent_name = M('category')->where("id='{$list['parent_id']}'")->field('id,name')->find();
    			$list['parent_name'] = $parent_name['name'];
    			$list['pre_parent_id'] = $parent_name['id'];
    			$list['parent_id'] = M('category')->where("id='{$list['pre_parent_id']}'")->getField('parent_id');
    		}
    		$this->assign('va',$list);
    	}
    	//一级分类
    	$where['parent_id'] = 0;
    	$where['is_del'] = 0;
    	$where['status'] = 0;
    	$res = lists('category','',$where,"id,name",'id desc', 0, 0,2);
    	$this->assign('res',$res);
    	//类型
    	$commond = M('commend')->where("status='0'")->order("sort desc")->select();
    	$this->assign('commend',$commond);
    	$this->display();
    }
    /**
     * 保存分类
     */
    public function save_class(){
    	$db = D('Category');
    	if($create = $db->create()){
    		$id = I('id/d');
    		$parent_id = I('parent_id');
    		$pre_parent_id = I('pre_parent_id');
    		if(empty($parent_id)){
    			$create['leve'] = 1;
    			$create['parent_id'] = 0;
    		}else{
    			if(empty($pre_parent_id)){
    				$create['leve'] = 2;
    				$create['parent_id'] = $parent_id;
    			}else{
    				$create['leve'] = 3;
    				$create['parent_id'] = $pre_parent_id;
    			}
    		}
     		if($id){
    			$create['id'] = $id;
    			$res = $db->save($create);
    		}else{
    			$log = "新增了一个商品分类 分类名为".$create['name'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			showmsg(200,'保存成功');
    		}else{
    			showmsg(300,'你没有做任何的修改 ，无需保存编辑');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 改变商品分类的显示方式
     */
    public function update_class_status(){
    	$id = I('id/d');
    	$db = M('category');
    	$res = $db->where("id='{$id}'")->field("name,status,leve")->find();
    	if($res['status'] == 1){
    		$db->where("id='{$id}'")->setField('status',0);
    		$this->update_next_class($id, $res['leve'], 'status', 0);
    		$log = "将商品分类名为".$res['name']."的状态有关闭变成开启";
    	}else{
    		$db->where("id='{$id}'")->setField('status',1);
    		$this->update_next_class($id, $res['leve'], 'status', 1);
    		$log = "将商品分类名为".$res['name']."的状态有开启变成关闭";
    	}
    	$this->add_log($log);
    	showmsg(200,'修改成功');
    }
    /**
     * 删除分类
     */
    public function del_goods_class(){
    	$id = I('id/d');
    	$idarray = explode(",",$id);
    	$db = M('category');
    	$re = $db->where("id='{$id}'")->field("name,leve")->find();
    	$db->where("id='{$id}'")->setField('is_del',1);
    	$log = "删除了一个商品分类，名为".$re['name'];
    	$this->update_next_class($id, $re['leve'], 'is_del', 1);
    	$this->add_log($log);
    	showmsg(200,'删除成功');
    }
    /**
     * 添加、编辑品牌
     */
    public function brand(){
    	$id = I('id/d');
    	if($id){
    		$map['id'] = $id;
    		$list = lists('brand','',$map,"*",'',0,0,1);
    		if(!empty($list['category_ids'])){
    			$list['category_ids'] = explode(",",$list['category_ids']);
    		}else{
    			$list['category_ids'] = array();
    		}
    		$this->assign('va',$list);
    	}
    	//商品分类
    	$where['status'] = 0;
    	$where['is_del'] = 0;
    	$res = lists('category','',$where,"id,name,parent_id",'id desc', 0, 0,2);
    	$res = \Org\Oa\common::sortdata($res,0,'&nbsp;&nbsp;&nbsp;&nbsp;');
    	$this->assign('res',$res);
    	$this->display();
    }
    /**
     * 保存品牌
     */
    public function save_brand(){
    	$db = D('Brand');
    	if($create = $db->create()){
    		$id = I('id/d');
    		$category_ids = I('category_ids');
    		if(!empty($category_ids)){
    			$create['category_ids'] = join(",",$category_ids);
    		}
    		if($id){
    			$create['id'] = $id;
    			$res = $db->save($create);
    		}else{
    			$log = "新增了一个品牌 品牌名为".$create['name'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			showmsg(200,'保存成功');
    		}else{
    			showmsg(300,'你没有做任何的修改 ，无需保存编辑');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 改变品牌的显示方式
     */
    public function update_brand_status(){
    	$id = I('id/d');
    	$db = M('brand');
    	$res = $db->where("id='{$id}'")->field("name,status")->find();
    	if($res['status'] == 1){
    		$db->where("id='{$id}'")->setField('status',0);
    		$log = "将品牌名为".$res['name']."的状态有关闭变成开启";
    	}else{
    		$db->where("id='{$id}'")->setField('status',1);
    		$log = "将品牌名为".$res['name']."的状态有开启变成关闭";
    	}
    	$this->add_log($log);
    	showmsg(200,'修改成功');
    }
    /**
     * 删除品牌
     */
    public function del_brand(){
    	$id = I('id');
    	$idarray = explode(",",$id);
    	$db = M('brand');
    	foreach ($idarray as $va){
    		$re = $db->where("id='{$va}'")->field("name")->find();
    		$log = "删除了一个商品分类，名为".$re['name'];
    		$this->add_log($log);
    		$db->where("id='{$va}'")->setField('is_del',1);
    	}
    	showmsg(200,'删除成功');
    }
    /**
     * 添加、编辑商品推荐类型
     */
    public function commend(){
    	$id = I('id/d');
    	if($id){
    		$map['id'] = $id;
    		$list = lists('commend','',$map,"*",'',0,0,1);
    		$this->assign('va',$list);
    	}
    	$this->display();
    }
    /**
     * 保存商品推荐类型
     */
    public function save_commend(){
    	$db = D('Commend');
    	if($create = $db->create()){
    		$id = I('id/d');
    		if($id){
    			$create['id'] = $id;
    			$res = $db->save($create);
    		}else{
    			$log = "新增了一个商品推荐类型 名为".$create['name'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			showmsg(200,'保存成功');
    		}else{
    			showmsg(300,'你没有做任何的修改 ，无需保存编辑');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 改变商品推荐类型的显示方式
     */
    public function update_commend_status(){
    	$id = I('id/d');
    	$db = M('commend');
    	$res = $db->where("id='{$id}'")->field("name,status")->find();
    	if($res['status'] == 1){
    		$db->where("id='{$id}'")->setField('status',0);
    		$log = "将商品推荐类型名为".$res['name']."的状态有关闭变成开启";
    	}else{
    		$db->where("id='{$id}'")->setField('status',1);
    		$log = "将商品推荐类型名为".$res['name']."的状态有开启变成关闭";
    	}
    	$this->add_log($log);
    	showmsg(200,'修改成功');
    }
    /**
     * 删除商品推荐类型
     */
    public function del_commend(){
    	$id = I('id');
    	$idarray = explode(",",$id);
    	$db = M('commend');
    	foreach ($idarray as $va){
    		$re = $db->where("id='{$va}'")->field("name")->find();
    		$log = "删除了一个商品推荐类型，名为".$re['name'];
    		$this->add_log($log);
    		$db->where("id='{$va}'")->setField('is_del',1);
    	}
    	showmsg(200,'删除成功');
    }
    /**
     * 添加、编辑商品
     */
    public function pro(){
    	$id = I('id/d');
    	//分类列表
    	/* $where['status'] = 0;
    	$where['is_del'] = 0;
    	$res = lists('category','',$where,"id,name,parent_id",'id desc', 0, 0,2);
    	$res = \Org\Oa\common::unlimitedForLevel($res,'&nbsp;&nbsp;&nbsp;&nbsp;');	
    	$this->assign('res',$res); */
    	
    	//城市列表
    	$cityarea['status'] = 0;
    	$city = lists('city','',$cityarea,'id,name,parent_id','id desc', 0,0, 2);
    	$city = \Org\Oa\common::unlimitedForLevel($city,'&nbsp;&nbsp;&nbsp;&nbsp;');
    	$this->assign('city',$city);
    	//品牌
    	$band = array();
    	$band['status'] = 0;
    	$brandlist = lists('brand','',$band,'id,name','sort desc', 0,0,2);
    	$this->assign('brandlist',$brandlist);
    	//推荐类型
    	$commend = lists('commend','',$band,'id,name','sort desc',0,0,2);
    	$this->assign('commend',$commend);
    	//商家列表
    	$shoplist = M('shop')->where("close='0' and is_check='1'")->field("id,shop_name as name")->select();
    	$this->assign('shoplist',$shoplist);
    	if($id){
    		$list = M('goods')->where("id='{$id}'")->find();
    		//$list['class_id'] = goods_class($id);
    		//推荐类型
    		$data['goods_id'] = $id; 
    		$commend_one = lists('commend_goods','',$data,'commend_id','id desc',0,0,2);
    		$list['commend_ids'] = array();
    		if(!empty($commend_one)){
    			foreach ($commend_one as $va){
    				$list['commend_ids'][] = $va['commend_id'];
    			}
    		}
    		
    		$list['photo'] = explode(",",$list['photo']);
    		//城市
    		$goodscitys = M('city_goods')->where($data)->field("city_id")->select();
    		$list['city_ids'] = i_array_column($goodscitys, 'city_id');
    		//分类
    		$category_ids_list = M('category_extend')->where($data)->field("category_id")->select();
    		$category_ids_list_array = i_array_column($category_ids_list, "category_id");
    		$list['category_ids'] = array();
    		$data['commend_id'] = $list['commend_id'];
    		$category_ids = lists('category','',$data,'id,name','id desc',0,0,2);
    		foreach ($category_ids as $i=>$vaa){
    			if(in_array($vaa['id'],$category_ids_list_array)){
    				$category_ids[$i]['ck'] = 1;
    			}else{
    				$category_ids[$i]['ck'] = 0;
    			}
    			//$list['category_ids'][] = $vaa['category_id'];
    		}
    		$list['category_ids'] = $category_ids;
    		$this->assign('va',$list);
    	}
    	$this->display();
    }
    /**
     * 商品介绍
     */
    public function proone(){
    	$id = I('id/d');
    	if($id){
    		$list = M('goods')->where("id='{$id}'")->field("content")->find();
    		$this->assign('va',$list);
    	}
    	$this->display();
    }
    /**
     * 商品模型
     */
    public function protwo(){
    	//模型
    	$data['is_del'] = 0;
    	$model = lists('model','',$data,'id,name','id desc',0,0,2);
    	$this->assign('model',$model);
    	$id = I('id/d');
    	if($id){
    		$list = M('goods')->where("id='{$id}'")->field("id,model_id")->find();
    		$this->assign('va',$list);
    	}
    	$this->display();
    }
    /**
     * 商品销售
     */
    public function prothree(){
    	$id = I('id/d');
    	if($id){
    		$list = M('goods')->where("id='{$id}'")->field("sale")->find();
    		$this->assign('va',$list);
    	}
    	$this->display();
    }
    /**
     * 保存商品
     */
    public function save_goods(){
    	$db = D('Common/Goods');
    	if($create = $db->create()){
    		$id = I('id/d');
    		$create['sale'] = $_POST['sale'];
    		if(!empty($_POST['content'])){
    			$create['content'] = $_POST['content'];
    		}
    		if($id){
    			$create['id'] = $id;
    			$db->save($create);
    			$res = $id;
    		}else{
    			$log = "新增了一个商品，商品名为：".$create['name'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		$db->save_spec($res);
    		$db->save_attr($res);
    		$db->save_category($res);
    		$db->save_commend($res); 
    		$db->save_product($res);
    		$db->save_city($res);
    	    showmsg(200,'保存成功');
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 改变商品的显示方式
     */
    public function update_goods_status(){
    	$id = I('id/d');
    	$db = M('goods');
    	$res = $db->where("id='{$id}'")->field("name,status")->find();
    	if($res['status'] == 1){
    		$db->where("id='{$id}'")->setField('status',0);
    		$log = "将商品名为".$res['name']."的状态有下架变成上架";
    	}else{
    		$db->where("id='{$id}'")->setField('status',1);
    		$log = "将商品名为".$res['name']."的状态有上架变成下架";
    	}
    	$this->add_log($log);
    	showmsg(200,'修改成功');
    }
    /**
     * 删除商品
     */
    public function del_goods(){
    	$id = I('id');
    	$idarray = explode(",",$id);
    	$db = M('goods');
    	foreach ($idarray as $va){
    		$re = $db->where("id='{$va}'")->field("name")->find();
    		$log = "删除了一个商品，名为".$re['name'];
    		$this->add_log($log);
    		$db->where("id='{$va}'")->setField('is_del',1);
    	}
    	showmsg(200,'删除成功');
    }
    /**
     * 改变商品的库存
     */
    public function sort_goods(){
    	$id = I('id/d');
    	$type = I('type');
    	$val = I('val');
    	$re = M('goods')->where("id='{$id}'")->setField($type,$val);
    	showmsg(200,'修改成功');
    }
    /**
     * 查看商品库存
     */
    public function pro_pro(){
    	$id = I('id/d');
    	$list = M('products')->where("goods_id='{$id}'")->field("id,products_no,sell_price,store_nums,spec_array,goods_id")->select();
    	$this->assign('list',$list);
    	$this->display();
    }
    /**
     * 改变货品的库存、销售价格
     */
    public function sort_product(){
    	$id = I('id/d');
    	$type = I('type');
    	$val = I('val');
    	$re = M('products')->where("id='{$id}'")->setField($type,$val);
    	showmsg(200,'修改成功');
    }
    /**
     * 编辑、添加团购、限购活动
     */
    public function groupbuy(){
    	$id = I('id/d');
    	if($id){
    		$where['gb.id'] = $id;
    		$join = "left join __PRODUCTS__ as p on p.id = gb.goods_id left join __GOODS__ as g on g.id = p.goods_id";
    		$list = lists('group_buying as gb',$join,$where,"gb.*,g.name as goods_name",'gb.id desc',0,0,1);
    		$this->assign('va',$list);
    	}
    	$this->display();
    }
    /**
     * 保存团购、限购活动
     */
    public function save_groupbuy(){
    	$db = D('GroupBuying');
    	if($create = $db->create()){
    		$id = I('id/d');
    		$goods_id = I('pro_id');
    		if(empty($goods_id)){
    			showmsg(300,'请选择活动商品');
    		}
    		if(empty($id)){
    			$re = \Org\Oa\groupbuy::groupbuy_is_out($create['type'], $goods_id);
    			if(!empty($re)){
    				showmsg(300,'此商品，已经有活动啦');exit();
    			}
    		}
    		$create['goods_id'] = $goods_id;
    		if($id){
    			$create['id'] = $id;
    			$res = $db->save($create);
    		}else{
    			$log = "新增了一个活动 名为".$create['name'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			showmsg(200,'保存成功');
    		}else{
    			showmsg(300,'你没有做任何的修改 ，无需保存编辑');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 改变活动的状态
     */
    public function update_buy_status(){
    	$id = I('id/d');
    	$db = M('group_buying');
    	$res = $db->where("id='{$id}'")->field("name,status")->find();
    	if($res['status'] == 1){
    		$db->where("id='{$id}'")->setField('status',0);
    		$log = "将活动名为".$res['name']."的状态有关闭变成开启";
    	}else{
    		$db->where("id='{$id}'")->setField('status',1);
    		$log = "将活动名为".$res['name']."的状态有开启变成关闭";
    	}
    	$this->add_log($log);
    	showmsg(200,'修改成功');
    }
    /**
     * 删除活动
     */
    public function del_buy(){
    	$id = I('id/d');
    	$db = M('group_buying');
    	$res = $db->where("id='{$id}'")->field("name,status,type")->find();
    	$log = "删除了一个活动，名为：".$res['name'];
    	$db->where("id='{$id}'")->delete();
    	$this->add_log($log);
    	showmsg(200,'删除成功');
    }
    /**
     * 添加、编辑优惠券
     */
    public function coupon(){
    	$id = I('id/d');
    	if($id){
    		$list = $db = M('ticket')->where("id='{$id}'")->find();
    		if($list['num'] == "-1"){
    			$list['num'] = "0";
    		}
    		$this->assign('va',$list);
    	}
    	$this->display();
    }
    /**
     * 保存优惠券
     */
    public function save_coupon(){
    	$db = D('Ticket');
    	if($create = $db->create()){
    		$id = I('id/d');
    		$num = I('num/d');
    		if($id){
    			$create['id'] = $id;
    			$res = $db->save($create);
    		}else{
    			$log = "新增了一个优惠券 名为".$create['name'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			showmsg(200,'保存成功');
    		}else{
    			showmsg(300,'你没有做任何的修改 ，无需保存编辑');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 删除优惠券
     */
    public function del_coupon(){
    	$id = I('id/d');
    	$db = M('ticket');
    	$res = $db->where("id='{$id}'")->field("name")->find();
    	$log = "删除了一个优惠券，名为：".$res['name'];
    	$db->where("id='{$id}'")->delete();
    	$this->add_log($log);
    	showmsg(200,'删除成功');
    }
    /**
     * 改变优惠券的状态
     */
    public function update_ticket_status(){
    	$id = I('id/d');
    	$db = M('ticket');
    	$res = $db->where("id='{$id}'")->field("name,status")->find();
    	if($res['status'] == 1){
    		$db->where("id='{$id}'")->setField('status',0);
    		$log = "将优惠券名为".$res['name']."的状态有关闭变成开启";
    	}else{
    		$db->where("id='{$id}'")->setField('status',1);
    		$log = "将优惠券名为".$res['name']."的状态有开启变成关闭";
    	}
    	$this->add_log($log);
    	showmsg(200,'修改成功');
    }
    /**
     * 生成优惠券码
     */
    public function getcode(){
    	$id = I('id/d');
    	$today = date("Y-m-d H:i:s");
    	$data['id'] = $id;
    	$data['start_time'] = array('elt',$today);
    	$data['end_time'] = array('egt',$today);
    	$data['status'] = 0;
    	$data['type'] = 4;
    	$list = M('ticket')->where($data)->find();
    	if(empty($list)){
    		showmsg(300,'此优惠券不存在');exit();
    	}
    	$this->assign('va',$list);
    	$this->display();
    }
    /**
     * 保存发送优惠券码
     */
    public function save_send_code(){
    	$id = I('id/d');
    	$num = I('num/d');
    	if(empty($num)){
    		showmsg(300,'请输入生成数量');exit();
    	}
    	$today = date("Y-m-d H:i:s");
    	$data['id'] = $id;
    	$data['start_time'] = array('elt',$today);
    	$data['end_time'] = array('egt',$today);
    	$data['status'] = 0;
    	$data['type'] = 4;
    	$list = M('ticket')->where($data)->find();
    	if(empty($list)){
    		showmsg(300,'此优惠券不存在');exit();
    	}
    	if(!empty($list['num'])){
    		if($num>($list['num']-$list['send_num'])){
    			showmsg(300,'输入的数量大于优惠券的数量');exit();
    		}
    	}
    	$ma = \Org\Oa\prefer::add_code($id, $num);
    	showmsg(200,join(",",$ma));
    }
    /**
     * 发送优惠券给用户
     */
    public function send_user_coupon(){
    	$tid = I('get.tid');
    	$id = I('id');
    	$idarray = explode(",",$id);
    	$num = count($idarray);
    	$today = date("Y-m-d H:i:s");
    	$data['id'] = $tid;
    	$data['start_time'] = array('elt',$today);
    	$data['end_time'] = array('egt',$today);
    	$data['status'] = 0;
    	$data['type'] = 1;
    	$list = M('ticket')->where($data)->find();
    	if(empty($list)){
    		showmsg(300,'此优惠券不存在');exit();
    	}
    	if(!empty($list['num'])){
    		if($num>($list['num']-$list['send_num'])){
    			showmsg(300,'选择用户的数量大于优惠券的数量');exit();
    		}
    	}
    	$ma = \Org\Oa\prefer::add_code($tid, $num);
    	if(!empty($ma[0])){
    		foreach ($ma as $i=>$va){
    			\Org\Oa\prefer::send_user_coupon($va, $idarray[$i]);
    		}
    		showmsg(200,'发送成功');
    	}else{
    		showmsg(300,'发送失败');
    	}
    }
    /**
     * 给用户组的用户发送优惠券
     */
    public function send_user_group_coupon(){
    	$tid = I('tid');
    	$today = date("Y-m-d H:i:s");
    	$data['id'] = $tid;
    	$data['start_time'] = array('elt',$today);
    	$data['end_time'] = array('egt',$today);
    	$data['status'] = 0;
    	$data['type'] = 1;
    	$list = M('ticket')->where($data)->find();
    	if(empty($list)){
    		showmsg(300,'此优惠券不存在');exit();
    	}
    	$group_id = I('lv/d');
    	$data['group_id'] = $group_id;
    	$data['status'] = 0;
    	$user = M('user')->where($data)->field("id")->select();
    	if(!empty($user)){
    		$num = count($user);
    		foreach ($user as $va){
    			$userlist[] = $va['id'];
    		}
    		if(!empty($list['num'])){
    			if($num>($list['num']-$list['send_num'])){
    				showmsg(300,'选择用户的数量大于优惠券的数量');exit();
    			}
    		}
    		$ma = \Org\Oa\prefer::add_code($tid, $num);
    		if(!empty($ma[0])){
    			foreach ($ma as $i=>$va){
    				\Org\Oa\prefer::send_user_coupon($va, $userlist[$i]);
    			}
    			showmsg(200,'发送成功');
    		}else{
    			showmsg(300,'发送失败');
    		}
    	}else{
    		showmsg(300,'发送失败');
    	} 
    }
    /**
     * 增加、编辑商品促销
     */
    public function prom(){
    	$id = I('id/d');
    	if($id){
    		$list = M('promotion')->where("id='{$id}' and type='0'")->find();
    		if(empty($list)){
    			showmsg(300,'此促销不存在');exit();
    		}
    		$where['go.is_del'] = 0;
    		$where['go.status'] = 0;
    		$where['pr.id'] = array('in',$list['goods_id']);
    		$join = "left join ".C('DB_PREFIX')."goods as go on go.id = pr.goods_id ";
    		$list['goods'] = lists('products as pr',$join,$where,"pr.spec_array,pr.store_nums,pr.sell_price,pr.id,go.name,go.img","pr.id desc",0,0,2);
    		$list['user_group'] = explode(",",$list['user_group']);
    		$this->assign('va',$list);
    	}
    	//优惠券
    	$coupon = M('ticket')->where("(num!='0' and num!=send_num) or (num = '0')")->field("id,name")->order("id desc")->select();
    	$this->assign('coupon',$coupon);
    	//用户组
    	$group = M('user_group')->order("id desc")->field("id,name")->select();
    	$this->assign('group',$group);
    	$this->display();
    }
    /**
     * 保存商品促销
     */
    public function save_promotion(){
    	$db = D('Promotion');
    	if($create = $db->create()){
    		$id = I('id/d');
    		if($id){
    			$create['id'] = $id;
    			$res = $db->save($create);
    		}else{
    			$log = "新增了一个促销规则 名为".$create['name'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			showmsg(200,'保存成功');
    		}else{
    			showmsg(300,'你没有做任何的修改 ，无需保存编辑');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 删除促销
     */
    public function del_prom(){
    	$id = I('id/d');
    	$db = M('promotion');
    	$res = $db->where("id='{$id}'")->field("name")->find();
    	$log = "删除了一个促销活动，名为：".$res['name'];
    	$db->where("id='{$id}'")->delete();
    	$this->add_log($log);
    	showmsg(200,'删除成功');
    }
    /**
     * 改变促销活动的状态
     */
    public function update_prom_status(){
    	$id = I('id/d');
    	$db = M('promotion');
    	$res = $db->where("id='{$id}'")->field("name,status")->find();
    	if($res['status'] == 1){
    		$db->where("id='{$id}'")->setField('status',0);
    		$log = "将促销活动名为".$res['name']."的状态有关闭变成开启";
    	}else{
    		$db->where("id='{$id}'")->setField('status',1);
    		$log = "将促销活动名为".$res['name']."的状态有开启变成关闭";
    	}
    	$this->add_log($log);
    	showmsg(200,'修改成功');
    }
    /**
     * 增加、编辑商品促销
     */
    public function orderprom(){
    	$id = I('id/d');
    	if($id){
    		$list = M('promotion')->where("id='{$id}' and type='1'")->find();
    		if(empty($list)){
    			showmsg(300,'此促销不存在');exit();
    		}
    		$list['user_group'] = explode(",",$list['user_group']);
    		$this->assign('va',$list);
    	}
    	//优惠券
    	$coupon = M('ticket')->where("(num!='0' and num!=send_num) or (num = '0')")->field("id,name")->order("id desc")->select();
    	$this->assign('coupon',$coupon);
    	//用户组
    	$group = M('user_group')->order("id desc")->field("id,name")->select();
    	$this->assign('group',$group);
    	$this->display();
    }
    /**
     * 
     */
    public function add_wx(){
    	$list = F('wx_basc');
    	$this->assign('va',$list);
    	$this->display();
    }
    /**
     * 保存微信基本配置
     */
    public function save_wx_basc(){
    	$basc = F('wx_basc')?F('wx_basc'):array();
    	$list = I('post.');
    	$newlist = array_merge($basc, $list);
    	F('wx_basc',$newlist);
    	showmsg(200,'保存成功');
    }
    /**
     * 删除公众号
     */
    public function del_wx(){
    	$list = F('wx_basc',null);
    	showmsg(200,'删除成功');
    }
    /**
     * 删除微信菜单
     */
    public function del_wx_menu(){
    	$id = I('id/d');
    	$db = M('wx_menu');
    	$res = $db->where("id='{$id}'")->field("name,parent_id")->find();
    	$log = "删除了一个微信菜单，名为：".$res['name'];
    	$db->where("id='{$id}'")->delete();
    	if($res['parent_id'] == 0){
    		$db->where("parent_id='{$id}'")->delete();
    	}
    	$this->add_log($log);
    	showmsg(200,'删除成功');
    }
    /**
     * 添加、编辑各种支付
     */
    public function pay(){
    	$type = I('type');
    	switch ($type){
    		case '1':
    			$list = F('set_zhifubao');
    			break;
    		case '2':
    			$list = F('set_zhifubao');
    			break;
    		case '3':
    			$list = F('set_weixin');
    			break;
    		case '4':
    			$list = F('set_yinlian');
    			break;
    		case '5':
    			$list = F('set_caifutong');
    		break;
    	}
    	$this->assign('va',$list);
    	$this->display();
    }
    /**
     * 添加、编辑商品推荐类型
     */
    public function keyword_list(){
    	$id = I('id/d');
    	if($id){
    		$map['id'] = $id;
    		$list = lists('keyword','',$map,"*",'',0,0,1);
    		$this->assign('va',$list);
    	}
    	$this->display();
    }
    /**
     * 保存搜素关键词
     */
    public function save_keyword(){
    	$db = D('Keyword');
    	if($create = $db->create()){
    		$id = I('id/d');
    		if($id){
    			$create['id'] = $id;
    			$res = $db->save($create);
    		}else{
    			$log = "新增了一个关键词 名为".$create['name'];
    			$this->add_log($log);
    			$res = $db->add($create);
    		}
    		if($res!=false){
    			showmsg(200,'保存成功');
    		}else{
    			showmsg(300,'你没有做任何的修改 ，无需保存编辑');
    		}
    	}else{
    		showmsg(300,$db->getError());
    	}
    }
    /**
     * 删除关键词
     */
    public function del_keyword(){
    	$id = I('id');
    	$idarray = explode(",",$id);
    	$db = M('keyword');
    	foreach ($idarray as $va){
    		$re = $db->where("id='{$va}'")->field("name")->find();
    		$log = "删除了一个关键词，名为".$re['name'];
    		$this->add_log($log);
    		$db->where("id='{$va}'")->setField('is_del',1);
    	}
    	showmsg(200,'删除成功');
    }
    /**
     * 删除搜素关键词
     */
    public function del_search(){
    	$id = I('id');
    	$idarray = explode(",",$id);
    	$db = M('search');
    	foreach ($idarray as $va){
    		$re = $db->where("id='{$va}'")->field("keyword")->find();
    		$log = "删除了一个搜素词，名为".$re['name'];
    		$this->add_log($log);
    		$db->where("id='{$va}'")->setField('is_del',1);
    	}
    	showmsg(200,'删除成功');
    }
    /**
     * 评论详情
     */
    public function comment(){
    	$id = I('id/d');
    	if($id){
    		$list = M('discussion')->where("id='{$id}' and parent_id='0'")->find();
    		if(!empty($list)){
    			$list['user'] = userinfo($list['user_id']);
    			$list['replay'] = M('discussion')->where("parent_id='{$list['id']}'")->find();
    			if(!empty($list['replay'])){
    				$list['replay']['adminname'] = M('admin')->where("id='{$list['replay']['user_id']}'")->getField('username');
    			}
    		}else{
    			showmsg(300,'参数错误');exit();
    		}
    		$this->assign('va',$list);
    		$this->display();
    	}else{
    		showmsg(300,'参数错误');exit();
    	} 
    }
    /**
     * 保存评论
     */
    public function save_comment(){
    	$id = I('id/d');
    	$db = M('discussion');
    	$list = $db->where("parent_id='{$id}'")->find();
    	if(!empty($list)){
    		showmsg(300,'你已经回复过啦,无需回复');exit();
    	}
    	$c = I('contents');
    	if(empty($c)){
    		showmsg(300,'请输入回复内容');exit();
    	}
    	$log = "回复了一条评论内容，内容为".$c;
    	$this->add_log($log);
    	$data['contents'] = I('contents');
    	$data['parent_id'] = $id;
    	$data['time'] = date("Y-m-d H:i:s");
    	$data['user_id'] = session('shopping_admin_id');
    	$db->add($data);
    	showmsg(200,'评论成功');exit();
    }
    /**
     * 改变评论显示的状态
     */
    public function update_comment_status(){
    	$id = I('id');
    	$db = M('discussion');
    	$type = I('get.type');
    	if($type){
    		if($type == 1){
    			$data['id'] = array('in',$id);
    			$res = $db->where($data)->setField('is_check',0);
    			showmsg(200,'修改成功');
    		}else{
    			$data['id'] = array('in',$id);
    			$res = $db->where($data)->setField('is_check',1);
    			showmsg(200,'修改成功');
    		}
    	}else{
    		$res = $db->where("id='{$id}'")->field("is_check")->find();
    		if($res['is_check'] == 1){
    			$db->where("id='{$id}'")->setField('is_check',0);
    			$log = "将一条评论的状态有显示变成隐藏";
    		}else{
    			$db->where("id='{$id}'")->setField('is_check',1);
    			$log = "将一条评论的状态有隐藏变成显示";
    		}
    		$this->add_log($log);
    		showmsg(200,'修改成功');
    	}
    	
    }
    /**
     * 删除评论
     */
    public function del_comment(){
    	$db = M('discussion');
    	$id = I('id');
    	$idarray = explode(",",$id);
    	$data['id'] = array('in',$idarray);
    	$res = $db->where($data)->delete();
    	showmsg(200,'修改成功');
    }
    /**
     * 删除用户
     */
    public function del_user(){
    	$id = I('id/d');
    	$list = M('user')->where("id='{$id}'")->field("mobile")->find();
    	$log = "删除了一个用户，用户名为:".$list['mobile'];
    	M('user')->where("id='{$id}'")->delete();
    	$this->add_log($log);
    	showmsg(200,'删除成功');
    }
    /**
     * 修改用户
     */
    public function member(){
    	$id = I('id');
    	if($id){
    		$list = M('user')->where("id='{$id}'")->find();
    		$this->assign('va',$list);
    		$address = M('address')->where("uid='{$id}'")->select();
    		$this->assign('address',$address);
    		$this->display();
    	}else{
    		showmsg(300,'用户错误');exit();
    	}
    }
    /**
     * 保存用户的基本资料
     */
    public function save_user_infor(){
    	$id = I('post.id');
    	if($id){
    		$data = I('post.');
    		$data['id'] = $id;
    		M('user')->save($data);
    		showmsg(200,'保存成功');
    	}else{
    	    showmsg(300,'保存失败');	
    	}
    }
    /**
     * 站内信
     */
    public function msg(){
    	$uid = I('uid');
    	if($uid){
    		$user['id'] = array('in',$uid);
    		$list= M('user')->where($user)->field("mobile")->select();
    		$ids = i_array_column($list, 'mobile');
    		$users['name'] = join(",",$ids);
    		$this->assign('user',$users); 
    	}
    	$this->display();
    }
    /**
     * 发送站内信
     */
    public function save_station(){
    	$uid = I('user_id');
    	if(empty($uid)){
    		showmsg(300,'请选择要发送的会员');exit();
    	}
    	$content = I('content');
    	if(empty($content)){
    		showmsg(300,'请输入站内信内容');exit();
    	}
    	$data['content'] = $content;
    	$res = M('station')->add($data);
    	if($res){
    		$uid = explode(",", $uid);
    		$uid = array_unique($uid);
    		foreach ($uid as $va){
    			$map['uid'] = $va;
    			$map['station_id'] = $res;
    			M('user_station')->add($map);
    		}
    		showmsg(200,'发送成功');exit();
    	}else{
    		showmsg(300,'发送失败');
    	}
    }
}


